package com.hl.card.award.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hl.card.award.service.IManualRewardByTrans;
import com.hl.card.award.service.IRewardByTrans;
import com.hl.card.common.remote.user.RemoteUser;
import com.hl.card.common.remote.user.RemoteUserService;
import com.hl.card.common.util.RandomStrUtils;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.common.util.PayUtils;
import com.hl.card.modular.system.service.IAccessRight;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * java类简单作用描述
 *
 * @ProjectName: distdev
 * @Package: com.hl.card.modular.pack.service.impl
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 作者姓名
 * @CreateDate: 2018/7/9 17:41
 * @UpdateUser: Neil.Zhou
 * @UpdateDate: 2018/7/9 17:41
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * <p>Copyright: Copyright (c) 2018</p>
 */
@Service
public class ManualRewardByTransImpl implements IManualRewardByTrans {



    private static final Logger logger = LoggerFactory.getLogger(ManualRewardByTransImpl.class);
    @Autowired
    private PayUtils payUtils;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private IRewardByTrans rewardByTrans;

    @Autowired
    private IAccessRight accessRight;

    @Value("${dismemberapply.ka_ip}")
    private String cpc_ip;

    @Value("${uc.getSMSCodeUrl}")
    private String ucGetSMSCodeUrl;

    public ResponseObject manualReward(String userId, String parentCode, String prodCode, String amount, String count, String clientIP, String account) {
        System.out.println("开始人工发放奖励");
        ResponseObject reObj=new ResponseObject();
        reObj.setErroCode(1);
        reObj.setData("非法操作,请重新获取授权");
        if(StringUtils.isBlank(prodCode)) {
            prodCode = parentCode;
        }
        if(StringUtils.isBlank(amount)) {
            amount = "0";
        }
        if(StringUtils.isBlank(count)) {
            count = "1";
        }
        int temp_count = Integer.parseInt(count);
        System.out.println("奖励倍数:" + temp_count);
        //if(accessRight.checkWxSysOptAccessRight(clientIP, account)) {
            if("WJ".equals(parentCode) || "ZHSC".equals(parentCode) || "CPTY".equals(parentCode)) {
                int amountInt = Integer.parseInt(amount);
                if(amountInt >= 100) {
                    amount = "100";
                }
            }
            reObj= rewardByTrans.manualReward(userId, parentCode, prodCode, amount, temp_count);
        //}
        return reObj;
    }

    public ResponseObject manualRewardByPhone(String mobile, String parentCode, String prodCode, String amount, String count, String clientIP, String account) {
        System.out.println("开始人工发放奖励");
        System.out.println("来访IP:" + clientIP);
        ResponseObject reObj=new ResponseObject();
        reObj.setErroCode(1);
        reObj.setData("非法操作,请重新获取授权");
        if(StringUtils.isBlank(prodCode)) {
            prodCode = parentCode;
        }
        if(StringUtils.isBlank(amount)) {
            amount = "0";
        }
        if(StringUtils.isBlank(count)) {
            count = "1";
        }
        int temp_count = Integer.parseInt(count);
        System.out.println("奖励倍数:" + temp_count);
        //if(accessRight.checkWxSysOptAccessRight(clientIP, account)) {
            ResponseObject res = new ResponseObject();
            String userId = null;
            res.setErroCode(1);
            res.setData("用户不存在");
            System.out.println("用户手机号:" + mobile);
            if(StringUtils.isNotBlank(mobile)) {
                JSONObject json = remoteUserService.findSsoUserByPhone(mobile);
                if (json.get("code").equals("0")) {
                    System.out.println("用户存在:" + mobile);
                    JSONObject userObj= (JSONObject) json.get("user");
                    RemoteUser remoteUser = JSON.parseObject(userObj.toJSONString(),RemoteUser.class);
                    if(remoteUser != null) {
                        userId = remoteUser.getUserId();
                        System.out.println("用户ID:" + userId);
                    }
                }else {
                    return res;
                }
            }
            if("WJ".equals(parentCode) || "ZHSC".equals(parentCode) || "CPTY".equals(parentCode)) {
                int amountInt = Integer.parseInt(amount);
                if(amountInt >= 100) {
                    amount = "100";
                }
            }
            reObj= rewardByTrans.manualReward(userId, parentCode, prodCode, amount, temp_count);
       // }
        return reObj;
    }

    public ResponseObject checkManualReward(String userId, String prodCode, String amount, String clientIP) {
        ResponseObject reObj=new ResponseObject();
        reObj.setErroCode(1);
        reObj.setData("非法用户");
        if (StringUtils.isNotBlank(amount)) {
            reObj= rewardByTrans.checkReward(userId, prodCode);
        }
        return reObj;
    }

    @Override
    public String checkUser(String userId) {
        return null;
    }

    public boolean getAccessRight(String phone, String rightCode, String clientIP) {
        String temp = stringRedisTemplate.opsForValue().get(phone + clientIP);
        if(StringUtils.isNotBlank(rightCode)) {
            if(rightCode.equals(temp)) {//验证码通过
                if(StringUtils.isNotBlank(clientIP)) {
                    //写入授权,如果使用"SUPER:MANUAL_ACCESS_IP" + clientIP为key，则表示可以同时授权给多台计算机
                    //stringRedisTemplate.opsForValue().set("SUPER:MANUAL_ACCESS_IP" + clientIP, clientIP,5, TimeUnit.MINUTES);//向redis里存入数据和设置缓存时间
                    //授权5分钟,5分钟内无操作，授权自动失效
                    stringRedisTemplate.opsForValue().set("SUPER:MANUAL_ACCESS_IP" + clientIP, clientIP, 24, TimeUnit.HOURS);
                    return true;
                }
            }
        }
        return false;
    }

    public boolean checkAccessRight(String clientIP) {
        String ip = stringRedisTemplate.opsForValue().get("SUPER:MANUAL_ACCESS_IP" + clientIP);
        System.out.println("授权IP:" + ip);
        System.out.println("来访IP:" + clientIP);
        if(StringUtils.isNotBlank(clientIP)) {//IP吻合
            if(clientIP.equals(ip)) {//允许操作
                //授权5分钟,5分钟内无操作，授权自动失效
                System.out.println("授权成功:" + clientIP);
                stringRedisTemplate.opsForValue().set("SUPER:MANUAL_ACCESS_IP" + clientIP, clientIP, 24, TimeUnit.HOURS);
                return true;
            }
        }
        System.out.println("授权失败:" + clientIP);
        return false;
    }

    public boolean sendSMSCode(String phoneNum, String clientIP) {
        String accessPhone = stringRedisTemplate.opsForValue().get("SUPER:ACCESS:ADMIN_PHONE:MANUAL_AWARD");
        if(StringUtils.isBlank(accessPhone)) {
            accessPhone = "18124021006";
        }
        if(StringUtils.isBlank(phoneNum) || StringUtils.isBlank(clientIP)) {
            System.out.println("手机号或者ip为空");
            return false;
        }
        String SMSCode = RandomStrUtils.getInstance().getRandomString();

        JSONObject result = remoteUserService.sendPCode(accessPhone, SMSCode);

        System.out.println(result.toJSONString());

        String resCode = result.getString("code");
        if("0".equals(resCode)) {
            stringRedisTemplate.opsForValue().set(phoneNum + clientIP, SMSCode,24, TimeUnit.HOURS);//向redis里存入数据和设置缓存时间
            return true;
        }
        return false;
    }
}
